package org.wddsicmd.widgets;
 
import org.wddsicmd.app.CodeEditorMain;
import org.wddsicmd.app.ButtonTabComponent;
import org.wddsicmd.util.ResourceBundleUtil;
import org.wddsicmd.util.Log;
import org.wddsicmd.util.Resource;

import java.lang.Object;

import java.io.*;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import java.util.*;
import java.util.Enumeration;
import java.util.Properties;
import java.util.Vector;

import java.awt.*;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.Insets;
import java.awt.Color;
import java.awt.Toolkit;
import java.awt.Image;
import java.awt.event.*;
import java.awt.Color;
import java.awt.Rectangle;
import java.awt.Container;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.*;
import javax.swing.text.*;
import javax.swing.event.*;
import javax.swing.Timer;
import javax.swing.JTable;
import javax.swing.table.*;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.Icon;
import javax.swing.JTree;
import javax.swing.UIManager;
import javax.swing.event.TreeModelListener;
import javax.swing.filechooser.FileSystemView;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.border.EmptyBorder;
import javax.swing.text.Document;

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.Paint;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import java.awt.geom.Area;
import java.awt.geom.Rectangle2D;
import java.awt.geom.RoundRectangle2D;

import javax.swing.Icon;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.plaf.basic.BasicTabbedPaneUI;
import javax.swing.plaf.basic.BasicGraphicsUtils;

public class ConnectionPanel extends JPanel implements ListSelectionListener{
      private final static String TAG = "ConnectionPanel";
	private DefaultListModel mDefaultListModel;
	private JScrollPane mScrollPane;
      private JList mList;

	private DeviceChangeListener mDeviceChangeListener;
	
	public ConnectionPanel(CodeEditorMain cem, String titleStr){
		super();
		
		setLayout(null);
		setBackground(Resource.getBackgroundColor());	
		addComponentListener(new ComponentAdapter() {
			@Override
			public void componentResized(ComponentEvent e) {
				onComponentResized();
			}
		});
		
		initList();
	}
	public void onComponentResized(){
		int width = getWidth();
		int height = getHeight();

/* doom-2015-11-2 15:2:0 
		Log.d(TAG, "onComponentResized width: "+ width);
		Log.d(TAG, "onComponentResized height: "+ height);
 doom-2015-11-2 15:2:0 */
		int contentwidth = width/4;
		int contentheight = height/2;

		mScrollPane.setBounds(new Rectangle((width- contentwidth)/2, (height- contentheight)/2, contentwidth, contentheight));
		
	}
	private void initList(){
		mDefaultListModel = new DefaultListModel();
		mList = new JList(mDefaultListModel);
		mList.addListSelectionListener(this);
 		mScrollPane = new JScrollPane(mList);		
		add(mScrollPane);
	}
	public void updateList(String[] deviceList){
		mDefaultListModel.removeAllElements();
		if(deviceList != null && deviceList.length > 0){
			for(int index = 0; index < deviceList.length; index++){
				mDefaultListModel.addElement(deviceList[index]);
			}
		}
	}
	
	public void addDevice(String deviceName){
		
/* doom-2015-11-2 16:11:7 
		Log.d(TAG, "addDevice mDeviceChangeListener: "+ mDeviceChangeListener);
		Log.d(TAG, "addDevice mDefaultListModel.getSize(): "+ mDefaultListModel.getSize());
 doom-2015-11-2 16:11:7 */
/* doom-2015-11-2 16:14:4 
		if(mDefaultListModel.getSize() == 0){
			if(mDeviceChangeListener != null){
				mDeviceChangeListener.deviceChanged(deviceName);
			}
		}
 doom-2015-11-2 16:14:4 */
		mDefaultListModel.addElement(deviceName);
/* doom-2015-11-2 16:11:5 
		Log.d(TAG, "addDevice mDefaultListModel.getSize(): "+ mDefaultListModel.getSize());
 doom-2015-11-2 16:11:5 */
		if(mDefaultListModel.getSize() == 1){
			mList.setSelectedIndex(0);
			if(mDeviceChangeListener != null){
				mDeviceChangeListener.deviceChanged((String)mDefaultListModel.getElementAt(0));
			}
		} 

	}
	public void removeDevice(String deviceName){
/* doom-2015-11-2 16:11:10 
		Log.d(TAG, "removeDevice mDeviceChangeListener: "+ mDeviceChangeListener);
		Log.d(TAG, "removeDevice mDefaultListModel.getSize(): "+ mDefaultListModel.getSize());
 doom-2015-11-2 16:11:10 */
 		boolean highlightReset = false;
		String highlightItem = (String)mList.getSelectedValue();
		if(highlightItem != null && highlightItem.equals(deviceName)){
			highlightReset = true;
		}
 		
		mDefaultListModel.removeElement(deviceName);
/* doom-2015-11-2 16:11:11 
		Log.d(TAG, "removeDevice mDefaultListModel.getSize(): "+ mDefaultListModel.getSize());
 doom-2015-11-2 16:11:11 */

		if(mDefaultListModel.getSize() == 1){
			if(mDeviceChangeListener != null){
				mDeviceChangeListener.deviceChanged((String)mDefaultListModel.getElementAt(0));
			}
		}else if(mDefaultListModel.getSize() == 0){
			if(mDeviceChangeListener != null){
				mDeviceChangeListener.deviceChanged(null);
			}
		}else if(highlightReset){
			if(mDeviceChangeListener != null){
				mDeviceChangeListener.deviceChanged((String)mDefaultListModel.getElementAt(0));
			}
		}


	}
	public void valueChanged(ListSelectionEvent e){
		if(!e.getValueIsAdjusting()){
/* doom-2015-11-2 15:31:4 
			Log.d(TAG, "valueChanged e.getValueIsAdjusting(): "+ e.getValueIsAdjusting());
			Log.d(TAG, "valueChanged mList.getSelectedIndex(): "+ mList.getSelectedIndex());
 doom-2015-11-2 15:31:4 */
			String serialNumber = (String)mList.getSelectedValue();
			Log.d(TAG, "valueChanged serialNumber: "+ serialNumber);
			if(serialNumber == null && mDefaultListModel.getSize() > 0){
				serialNumber = (String)mDefaultListModel.getElementAt(0);
			}
			if(mDeviceChangeListener != null){
				mDeviceChangeListener.deviceChanged(serialNumber);
			}
		}
	}

	public interface DeviceChangeListener{
		void deviceChanged(String serialNumber);
	}	
	public void addDeviceChangeListener(DeviceChangeListener dc){
		mDeviceChangeListener = dc;
	}	
}
